layui.use(["table",'form',"util","layer"],function () {
    var table = layui.table;
    var util = layui.util;
    var form = layui.form;
    var layer = layui.layer;
    var $ = layui.jquery;

    var dictMaxTypeId = $("#dictMaxTypeId").val(); //传入的字典大类id.
    //
    var showdictTable = function() {
        table.render({
            elem: '#dictTable'
            , height: 350
            , url: '/dict/getDictListByTypeId?typeId=' + dictMaxTypeId //数据接口
            , cols: [[ //表头
                {field: 'id', title: 'ID',width:'15%'}
                , {field: 'type', title: '名称',width:'30%'}
                , {field: 'etype', title: '备注',width:'30%'}
                , {title: '操作',width:'25%',toolbar:"#barDemo"}
            ]]
        });
    };
    showdictTable();
    //触发事件(添加字典大类)
    $("#btnSumbit").click(function () {
        //保存字典大类。
        var type  = $("#type").val();
        var etype = $("#etype").val();
        var data = {
            type:type,
            etype:etype,
            typeId:$("#dictMaxTypeId").val()
        }
        var typeId   = $("#dictMaxTypeId").val();
        var typeName = $("#dictMaxTypeName").val();
        $.ajax({
            url:"/dict/saveDict",
            type:"post",
            data:data,
            success:function (res) {
                res = eval("(" + res+ ")");
                layer.msg(res.msg); //保存结果
                if(res.resultCode == 0){ //save OK .
                    setTimeout(function () { //重新加载。页面刷新。
                        window.location = '/dict/dictsub?id='+ typeId + "&name=" + typeName;
                    },300);
                }
            }
        });
    });
    table.on('tool(dictSubTable)', function (obj) {
        var data = obj.data; //当前行的数据。对象类型。
        if (obj.event === 'del') {
            layer.confirm('真的删除字典数据吗？', function (index) {
                var param = {
                    id:data.id
                };
                $.ajax({
                    url:"/dict/delDict",
                    type:"post",
                    data:param,
                    success:function (res) {
                        res = eval("(" + res+ ")");
                        layer.msg(res.msg); //保存结果
                        if(res.resultCode == 0){ //save OK .
                            obj.del(); //从画面上删除行
                        }
                    }
                });
            });
        }
    });
});



